<template>
  <div class="login_container">
    <div class="box">
      <div class="xiaobox">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="login_form">
        <!-- 账户 -->
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="icon iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" prefix-icon="icon iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">立即创建</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset() {
      this.$refs.form.resetFields()
    },
    login() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.form)
        // console.log(res)

        if (res.meta.status != 200) {
          this.$message.error('登录失败')
          return
        }
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
        this.$message.success('登录成功')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: hotpink;
  height: 100%;
}
.box {
  position: absolute;
  width: 500px;
  height: 350px;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .xiaobox {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 0 10px #eee;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid red;
    padding: 10px;
  }
  img {
    border-radius: 50%;
    background-color: #eee;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
